class ArrayList {
  constructor() {
    this.arrList = []
  }
  insert(arrItem) {
    this.arrList.push(arrItem)
  }
  toString() {
    return this.arrList.join('-')
  }
  // 冒泡排序
  BubbleSort() {
    for (let j = this.arrList.length - 1; j >= 0; j--) {
      for (let i = 0; i < j; ++i) {
        if (this.arrList[i] > this.arrList[i + 1]) {
          const temp = this.arrList[i]
          this.arrList[i] = this.arrList[i + 1]
          this.arrList[i + 1] = temp
        }
      }
    }
  }
  // 选择排序
  SelectSort() {
    for (let j = 0; j < this.arrList.length - 1; ++j) {
      let min = j
      for (let i = min + 1; i < this.arrList.length; ++i) {
        if (this.arrList[min] > this.arrList[i]) {
          min = i
        }
      }
      this.swap(min, j)
    }
  }
  swap(i, j) {
    let temp = this.arrList[i]
    this.arrList[i] = this.arrList[j]
    this.arrList[j] = temp
  }
}
const list = new ArrayList()
list.insert(1)
list.insert(4)
list.insert(3)
list.insert(0)
list.insert(8)
list.insert(2)

console.log(list.toString())
// list.BubbleSort()
list.SelectSort()
console.log(list.toString())
